package com.kharthick.pwGame.units;

import java.util.Vector;
import java.awt.Graphics;
import java.awt.MediaTracker;

import com.kharthick.game.Core;
import com.kharthick.pwGame.sprites.Sprite;
import com.kharthick.pwGame.sprites.SpriteFactory;

public abstract class AbstractUnitBackup implements Unit {
	protected String name;		// name of unit (unused)
	protected String type;
	private boolean active;		// is unit active?
	private boolean moveActive;
	
	protected int health;
	protected int attack;
	protected int defense;
	protected int range;
	protected int movement;
	
	private Sprite sprite;
	private Sprite idle;
	private Sprite walkLeft;
	private Sprite walkRight;
	private Sprite walkDown;
	private Sprite walkUp;
	private final Sprite deathSprite;
	
	private Vector<Integer> position;
	
	private int displaceX = 0, displaceY = 0;
	private int count;
	private int directionX, directionY;
	private int closeX, closeY;
	private int tX, tY;
	
	AbstractUnitBackup(String filename, String left, String right, 
			String down, String up, Vector<Integer> pos, MediaTracker tracker) {
		idle = SpriteFactory.getSprite(filename, tracker);
		walkLeft = SpriteFactory.getSprite(left, 5, 50, 50, tracker);
		walkRight = SpriteFactory.getSprite(right, 5, 50, 50, tracker);
		walkDown = SpriteFactory.getSprite(down, 5, 50, 50, tracker);
		walkUp = SpriteFactory.getSprite(up, 5, 50, 50, tracker);
		sprite = idle;
		deathSprite = SpriteFactory.getSprite("dead.png", tracker);
		
		position = pos;	
	}
	
	public void activate() {
		// Turn on the unit
		active = true;
	}

	public void deactivate() {
		// Turn off the unit
		active = false;
	}
	
	public boolean isActive(){
		return active;
	}
	
	public void activateMove(int targetX, int targetY){
		System.out.println("moveactivated");
		count = 1;
		closeX = position.get(0);
		closeY = position.get(1);
		tX = targetX;
		tY = targetY;
		//displaceX = -50;
		displaceY = -50;
		System.out.println(closeX + " " + closeY + " " + tX + " " + tY);
		moveActive = true;
		//move = new Thread(this);
		//core.start();
	}
	//public 
	public void moveAnimation(){
		
		//int draw;
		int scrollX = 0, scrollY = 0;
		
		directionX = tX-closeX;
		directionY = tY-closeY;
		//int aX = closeX - scrollX, aY = closeY - scrollY;
		//int hX = tX - scrollX, hY = tY - scrollY;
		
		/*if(count == 4){
			draw = 0;
		}
		else if(count > 4){
			draw = count-2;
		}
		else{
			draw = count-1;
		}*/
		if(directionX  > 0){	
			if(closeX*Core.TILE_SIZE + 10*count < tX*Core.TILE_SIZE+10){
				sprite = walkRight;
				displaceX = 10*count;
				//buffer.drawImage(animeright[draw], aX*TILE_SIZE + 10*count ,aY*TILE_SIZE,this);
			}
			else{
				System.out.println("it happens");
				sprite = walkDown;
				displaceX = 10*(count-1);
				//buffer.drawImage(animemove[draw], aX*TILE_SIZE + 10*(count-1),aY*TILE_SIZE,this);
			}
			if(count == 6){
				closeX++;
				count = 1;
			}
			else
				count++;
			displaceX -= 50;
		}
		else if(directionX  < 0){
			sprite = walkLeft;
			displaceX = -(10*count);			
			//buffer.drawImage(animeleft[draw], aX*TILE_SIZE - 10*count,aY*TILE_SIZE,this);
			if(count == 6){
				closeX--;
				count = 1;
			}
			else
				count++;
			displaceX -= 50;
			
		}
		else if(directionY  > 0){
			if(closeY*Core.TILE_SIZE + 10*count < tY*Core.TILE_SIZE){
				sprite = walkDown;
				displaceY = 10*count;
			//buffer.drawImage(animemove[draw], aX*TILE_SIZE,aY*TILE_SIZE + 10*count,this);
			}
//keep an eye out for this
			else{
				sprite = walkDown;
				displaceY = 10*(count-1);
				//buffer.drawImage(animemove[draw], aX*TILE_SIZE,aY*TILE_SIZE + 10*(count-1),this);
			}
//ish
			if(count == 6){
				closeY++;
				count = 1;
			}
			else
				count++;
			
			displaceY -= 50;
		}
		else if(directionY  < 0){
			sprite = walkUp;
			displaceY = -(10*count);
			//buffer.drawImage(animeback[draw], aX*TILE_SIZE,aY*TILE_SIZE - 10*count,this);
			if(count == 6){
				closeY--;
				count = 1;
			}
			else
				count++;
			
			displaceY -= 50;
			
		}
		else{
			sprite = idle;
			moveActive = false;
			//drawUnits('i');
			//phase = 1;
		}
		System.out.println(displaceX + " " + displaceY );
//fix
/*		drawUnits('m');
		if(aY*TILE_SIZE + 10*count > mainY-50){
			buffer.drawImage(ui, 120, 400, this);
			if(aX*TILE_SIZE < 120){
				drawMini();
			}
		}*/
	}

	public void draw(Graphics buf) {
		// Draw the unit
		if(moveActive == true){
			moveAnimation();
		}
		buf.drawImage(sprite.getImage(),	// the actual tile
				Core.TILE_SIZE*position.get(0) + displaceX,	// x position
				Core.TILE_SIZE*position.get(1) + displaceY,  // y position
				null);
	}
	
	public Vector<Integer> getPosition(){
		return position;		
	}
	
	public void setPosition(int row, int col){
		position.set(0, new Integer(row));
		position.set(1, new Integer(col));
	}
	
	public void setHp(int Hp){
		if(Hp > 0)
			health = Hp;
		else
			sprite = deathSprite;
	}
	
	public int getRange(){
		return range;
	}
	
	public int getMovement(){
		return movement;
	}
	
	public int getAttack(){
		return attack;
	}
	
	public int getHp(){
		return health;
	}
	
	public String getType(){
		return type;
	}
	
	public void leftClick(){
		System.out.println("clicked on unit");
		if(active = true) deactivate();
		else activate();
	}
	
}
